package com.sitech.ibnms.c3p.task.notice;

import com.sitech.ibnms.c3p.domain.C3pTaskNode;
import com.sitech.ibnms.c3p.domain.C3pTaskResult;
import com.sitech.ibnms.c3p.mappers.C3pNoticeMapper;
import com.sitech.ibnms.c3p.util.C3pConstants;
import com.sitech.ibnms.c3p.util.JSONUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2015/12/17.
 */
@Service
public class NoticeHandleService {
    Logger logger= LoggerFactory.getLogger(NoticeHandleService.class);
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private C3pNoticeMapper c3pNoticeMapper;
    public void sendStartNodeNotice(C3pTaskNode node,int serialNo){
        String smsType=node.getSmsType();
        try{
            if(StringUtils.isEmpty(node.getSmsGroup())){
                return;
            }
            if(C3pConstants.SMS_TYPE_BEFORE.equals(smsType)||C3pConstants.SMS_TYPE_ALL.equals(smsType)){
                List<String> smsList = c3pNoticeMapper.selectNoticeById(node.getSmsGroup(), "SMS");
                if(smsList!=null){
                    for (String sms : smsList) {
                        if (StringUtils.isNotEmpty(sms)) {
                            noticeService.sendMessage(sms.split(","), serialNo, "【" + node.getName() + "】环节开始执行");
                        }
                    }
                }
            }
        }catch (Exception e){
            logger.error("sendStartNodeNotice error",e);
        }

    }

    public void sendEndNodeNotice(C3pTaskNode node,int serialNo,boolean ifSuccess){
        String smsType=node.getSmsType();
        try{
            if(StringUtils.isEmpty(node.getSmsGroup())){
                return;
            }
            if(C3pConstants.SMS_TYPE_AFTER.equals(smsType)||C3pConstants.SMS_TYPE_ALL.equals(smsType)){
                List<String> smsList = c3pNoticeMapper.selectNoticeById(node.getSmsGroup(), "SMS");
                if(smsList!=null){
                    for (String sms : smsList) {
                        if(StringUtils.isNotEmpty(sms)){
                            noticeService.sendMessage(sms.split(","),serialNo,
                                    "【"+node.getName()+"】环节执行"+(ifSuccess?"成功":"异常"));
                        }
                    }
                }
            }
        }catch (Exception e){
            logger.error("sendEndNodeNotice error",e);
        }

    }
    public void sendTaskResultNotice(C3pTaskResult taskResult,String smsType,String message){
        try{
            logger.info("noticeService.sendMessage:"+message);
            if(message==null||message.isEmpty()){
                return;
            }
            List<String> smsList = c3pNoticeMapper.selectNoticeByTaskId(taskResult.getTaskId(),smsType);
            logger.info("smsList:"+smsList);
            logger.info("smsListJson:"+ JSONUtil.toJSON(smsList));
            if(smsList!=null){
                for (String sms : smsList) {
                    if (StringUtils.isNotEmpty(sms)) {
                        noticeService.sendMessage(sms.split(","), taskResult.getSerialNo(),message);
                    }
                }
            }
        }catch (Exception e){
            logger.error("sendTaskResultNotice error",e);
        }
    }
}
